package com.communication.service;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.communication.json.CommunicationEntity;
import com.communication.json.JsonResult;
import com.data.service.ItemService;
import com.entity.Item;
import com.entity.Measuringpoint;
import com.file.service.MeasuringpointService;
import com.file.service.MeterService;
import com.file.service.TerminalService;
import com.protocolmeter.ProtocolMerterService;
import com.protocolterminal.ProtocolTerminalService;
import com.protocolterminal.base.Seq;

@Component
public class CommunicationMeter {

	@Autowired
	private TerminalService terminalservice;
	@Autowired
	private ItemService itemService;
	@Autowired
	private MeasuringpointService measuringpointService;
	@Autowired
	private MeterService meterService;

	public String Enframe(JsonResult<CommunicationEntity> jsonResult) {

		// 获取信息
		String terminalprotocol = terminalservice
				.findTerminalprotocolByTerminalid(Integer.valueOf(jsonResult.getData().getTerminalID()));
		String terminaladdress = terminalservice
				.findTerminaladdressByTerminalid(Integer.valueOf(jsonResult.getData().getTerminalID()));
		Measuringpoint measuringpoint = measuringpointService.findByTerminalidAndMeterid(
				Integer.valueOf(jsonResult.getData().getTerminalID()),
				Integer.valueOf(jsonResult.getData().getMeterID()));
		String meteraddress = meterService
				.findMeteraddressByMeterid(Integer.valueOf(jsonResult.getData().getMeterID()));
		String meterprotocol = meterService
				.findMeterprotocolByMeterid(Integer.valueOf(jsonResult.getData().getMeterID()));
		// 查询item
		List<Item> items = itemService.listItemByDateitemid(Integer.valueOf(jsonResult.getData().getIdentification()));

		Item item = items.get(0);
		// 组帧
		ProtocolMerterService merterService = new ProtocolMerterService();
		String datas = merterService.Encode(Integer.valueOf(meterprotocol), meteraddress, item.getMetercontrol(),
				item.getIdentification(), "00000000", jsonResult.getData().getData(), "98000000", "11111111");

		datas = measuringpoint.getMeasuringpointport() + "-" + measuringpoint.getMeasuringpointbaud()
				+ "-0-1-0-8-100-0-100-" + datas;

		String das = "0000";
		if (!item.getDa().equals("0000")) {
			das = measuringpoint.getMeasuringpointnumber();
		}

		ProtocolTerminalService protocolTerminalService = new ProtocolTerminalService();
		String sentframe = protocolTerminalService.Encode(Integer.valueOf(terminalprotocol), item.getAfn(),
				new Seq(0, 1, 1, 1, 0).toString(), Arrays.asList(das.split("-")),
				Arrays.asList(item.getDt().split("-")), Arrays.asList(datas.split(";")),
				"00000000000000000000000000000000", 1, item.getPRM(), terminaladdress);

		return sentframe;
	}

	public JsonResult<CommunicationEntity> DeFrame(JsonResult<CommunicationEntity> jsonResult, String sentdata,
			String returndata) {
		JsonResult<CommunicationEntity> json = jsonResult;

		String terminalprotocol = terminalservice
				.findTerminalprotocolByTerminalid(Integer.valueOf(jsonResult.getData().getTerminalID()));
		// 比对帧
		ProtocolTerminalService protocolTerminalService = new ProtocolTerminalService();
		String decodedata = protocolTerminalService.Decode(Integer.valueOf(terminalprotocol), sentdata, returndata);

		CommunicationEntity communicationEntity = json.getData();
		if (decodedata == null) {
			communicationEntity.setData("数据无法解析");
		} else if (decodedata.equals("")) {
			communicationEntity.setData("返回数据为空");
		} else {
			// 解析表
			String meterprotocol = meterService
					.findMeterprotocolByMeterid(Integer.valueOf(jsonResult.getData().getMeterID()));
			// 解析
			ProtocolMerterService merterService = new ProtocolMerterService();
			String decodemeter = merterService.Decode(Integer.valueOf(meterprotocol), sentdata, decodedata);
			communicationEntity.setData(decodemeter);
		}
		json.setData(communicationEntity);

		return json;
	}

}
